$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/bteamrelation/listForTeamId',
        datatype: "json",
        colModel: [
            // {label: '团队ID', name: 'teamId', index: 'team_id', align: "center", width: 80,
            //     formatter: function (value, options, rowData) {
            //         var teamId = rowData["teamId"];
            //         return "<a href = bteamrelation.html?teamId=" + teamId + ">" + teamId + "</a>"
            //     }},
            // {label: '团队名称', name: 'teamName', index: 'team_name', align: "center", width: 80},
            // {label: '团队人数', name: 'total', index: 'total', align: "center", width: 80},
            {label: '用户ID', name: 'userId', index: 'user_id', align: "center", width: 80},
            {label: '用户昵称', name: 'nickName', index: 'nick_name', align: "center", width: 80},
            {
                label: '用户头像',
                name: 'avatarUrl',
                index: 'avatar_url',
                width: 80,
                align: "center",
                sortable: false,
                editable: false,
                formatter: function (value, options, rowData) {
                    var url = "https://lktvtool.oss-cn-shenzhen.aliyuncs.com/icon/236A5FF0-52DE-4644-B66C-E9555C97693D.png";
                    if (value == null || value == "") {
                        return "<img src='" + url + "' width='60' />";
                    } else {
                        return "<img src='" + value + "' width='60' />";
                    }
                }
            },
            {label: '公众号', name: 'gzhName', index: 'gzh_name', align: "center", width: 80},
            {label: '邀请人', name: 'inviterId', index: 'inviter_id', align: "center", width: 80},
            {
                label: '角色', name: 'teamRole', index: 'team_role', align: "center", width: 80,
                formatter: function (value, options, rowData) {
                    var content = "";
                    if (value == 0) {
                        content = '普通成员';
                    } else if (value == 1) {
                        content = '管理员';
                    } else if (value == 2) {
                        content = '队长';
                    }
                    return content;
                }
            },
            {label: '加入时间', name: 'createTime', index: 'create_time', align: "center", width: 80},
        ],
        viewrecords: true,
        height: 520,
        rowNum: 15,
        rowList: [15, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        postData: {'teamId': getQueryStringByName('teamId')},
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

// 根据参数名获取参数值
function getQueryStringByName(name) {
    var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
};

setTimeout(function (){
    $.get(baseURL + "sys/bteamrelation/infoForTeamId/" + getQueryStringByName('teamId'), function (r) {
        vm.teamInfoName = r.teamInfoName;
        vm.teamInfoNum = r.teamInfoNum;
    });
}, 500);

var vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            bTeamRelation: {},
            teamInfoName: "",
            teamInfoNum: 1
        },
        methods: {
            query: function () {
                vm.reload();
            },
            add: function () {
                vm.showList = false;
                vm.title = "新增";
                vm.bTeamRelation = {};
            },
            update: function (event) {
                var id = getSelectedRow();
                if (id == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";

                vm.getInfo(id)
            },
            saveOrUpdate: function (event) {
                var url = vm.bTeamRelation.id == null ? "sys/bteamrelation/save" : "sys/bteamrelation/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.bTeamRelation),
                    success: function (r) {
                        if (r.code === 0) {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            },
            del: function (event) {
                var ids = getSelectedRows();
                if (ids == null) {
                    return;
                }

                confirm('确定要删除选中的记录？', function () {
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/bteamrelation/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                alert('操作成功', function (index) {
                                    $("#jqGrid").trigger("reloadGrid");
                                });
                            } else {
                                alert(r.msg);
                            }
                        }
                    });
                });
            },
            getInfo: function (id) {
                $.get(baseURL + "sys/bteamrelation/info/" + id, function (r) {
                    vm.bTeamRelation = r.bTeamRelation;
                });
            },
            reload: function (event) {
                vm.showList = true;
                var page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    page: page,
                    postData: {'teamId': getQueryStringByName('teamId')}
                }).trigger("reloadGrid");
            }
        }
    })
;
